/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package configuration;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.DispatcherServlet;

import dynamic.DynamicDataSourceAspect;
import lombok.extern.log4j.Log4j;

/**
 * @author Mht
 * @date May 10, 2016
 */
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAsync
@EnableScheduling
@Import({ DynamicDataSourceRegister.class, RedisConfiguration.class, QuartzConfiguration.class
	,ExceptionConfiguraion.class, RestServiceConfiguration.class, WebMvcConfiguration.class,SettingConfiguration.class })
@ComponentScan({ "dao", "service", "schedule", "common" })
@ServletComponentScan({ "servlet" })
@Log4j
public class BootAppConfiguration extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {

	public BootAppConfiguration() {
		// TODO Auto-generated constructor stub
		log.info("[Initialize Boot Application]");
		Locale.setDefault(Locale.US);
	}

	@Bean
	public DynamicDataSourceAspect dynamicDataSourceAspect() {
		return new DynamicDataSourceAspect();
	}

//	@Bean  
	public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {  
	    ServletRegistrationBean registration = new ServletRegistrationBean(  
	            dispatcherServlet);  
	    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);  
	    return registration;  
	}  
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.boot.context.embedded.
	 * EmbeddedServletContainerCustomizer#customize(org.springframework.boot.
	 * context.embedded.ConfigurableEmbeddedServletContainer)
	 */
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		// TODO Auto-generated method stub
		container.setPort(8098);
		container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/jsp/error.jsp"));
		container.setSessionTimeout(10, TimeUnit.MINUTES);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(BootAppConfiguration.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(BootAppConfiguration.class, args);
	}
}
